package com.animations;

import com.beans.BirdBean;
import com.beans.MedalsBoardBean;
import com.beans.NewBean;
import com.beans.PersonBean;
import creator.ui.controller.ReadData;
import creator.ui.controller.VoicePlayer;
import creator.ui.controller.WriteData;
import creator.ui.graphics.Magicanvas;
import creator.ui.graphics.Paintools;
import creator.ui.graphics.bean.Switch;
import creator.ui.graphics.polaroid.Animation;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class MedalsBoardAction extends Animation {

  private MedalsBoardBean mbb;
  private NewBean news;
  private static final String FilePath = "/Scores.dat";
  private static ArrayList<PersonBean> Persons;
  private static final Font DefaultFont = new Font("tama mini02", 0, 29);

  private static int MaxScores = 40;
  private static List<PersonBean> peps;
  private boolean comeIn = false;
  private boolean disScr = false;
  private boolean disScrOver = false;
  private boolean medalsOk = false;
  private boolean isNew = false;
  private int currentScore = 0;
  private Image MEDALS;
  private Image star;
  private int best_s;
  private int starX;
  private int starY;

  public void loadBean() {
    this.mbb = new MedalsBoardBean();
    this.news = new NewBean();
    getBeans().add(this.mbb);
    getBeans().add(this.news);
  }

  public void paint(Graphics g) {
    if (!BirdBean._isAlive()) {
      if (this.comeIn) {
        g.drawImage(
          MedalsBoardBean.getMEDALS_BOARD(),
          MedalsBoardBean.getMEDALS_BOARD_X(),
          MedalsBoardBean.getMEDALS_BOARD_Y(),
          getMagicanvas()
        );
      }

      if (this.disScr) {
        int offset =
          LocationCalibration0(this.currentScore) +
          LocationCalibration1(this.currentScore);
        Paintools.drawShadowString(
          this.currentScore,
          MedalsBoardBean.getMEDALS_BOARD_X() + 191 + offset,
          MedalsBoardBean.getMEDALS_BOARD_Y() + 52,
          DefaultFont,
          2,
          Color.WHITE,
          Color.BLACK,
          g
        );
      }

      if (this.disScrOver) {
        int offset =
          LocationCalibration0(this.best_s) + LocationCalibration1(this.best_s);
        Paintools.drawShadowString(
          this.best_s,
          MedalsBoardBean.getMEDALS_BOARD_X() + 191 + offset,
          MedalsBoardBean.getMEDALS_BOARD_Y() + 94,
          DefaultFont,
          2,
          Color.WHITE,
          Color.BLACK,
          g
        );
      }

      if (this.MEDALS != null) {
        g.drawImage(
          this.MEDALS,
          MedalsBoardBean.getMEDALS_X(),
          MedalsBoardBean.getMEDALS_Y(),
          getMagicanvas()
        );
      }

      if (this.medalsOk) {
        g.drawImage(this.star, this.starX, this.starY, getMagicanvas());
      }

      if (this.isNew) g.drawImage(
        this.news.getNews(),
        MedalsBoardBean.getMEDALS_BOARD_X() + 135,
        MedalsBoardBean.getMEDALS_BOARD_Y() + 59,
        getMagicanvas()
      );
    }
  }

  private int LocationCalibration0(int num) {
    int offset = 0;
    if (num > 9) offset = -13; else if (num > 99) {
      offset = -26;
    }
    return offset;
  }

  private int LocationCalibration1(int num) {
    int offset = 0;
    if ((num == 1) || (num / 10 % 10 == 1) || (num / 100 % 10 == 1)) {
      offset = 6;
      if (num % 11 == 0) {
        offset = 10;
      }
      if (num % 111 == 0) {
        offset = 14;
      }
    }
    return offset;
  }

  public void run() {
    try {
      while (true) {
        if ((!BirdBean._isAlive()) && (OverPicAction.isOver)) {
          Thread.sleep(285L);
          LoadLocalData();
          DisplayBoard();
          LoadSocres();
          WhichMEDALS();
          AppendNewPerson(Persons);
          break;
        }
        Thread.sleep(500L);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void DisplayBoard() {
    int finalY = MedalsBoardBean.getMEDALS_BOARD_Y();
    int initY = 614 + finalY + 5;
    this.disScrOver = true;
    VoicePlayer.play("/res/sounds/sfx_swooshing.wav");
    do {
      MedalsBoardBean.setMEDALS_BOARD_Y(initY);
      try {
        Thread.sleep(2L);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      getMagicanvas().repaint();

      if (
        (MedalsBoardBean.getMEDALS_BOARD_Y() < 614) && (!this.comeIn)
      ) this.comeIn = true;
      initY -= 3;
    } while (initY >= finalY);
  }

  private void LoadLocalData() {
    String uri = getClass()
      .getResource("/res")
      .getPath()
      .replace("file:/", "")
      .replace("FlappyBird.jar!/", "")
      .replace("/res", "/Scores.dat");
    File sf = new File(uri);
    if (!sf.exists()) {
      try {
        sf.createNewFile();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    Persons = (ArrayList) ReadData.ReadObjectFromLocal("/Scores.dat");
    if (Persons != null) {
      getMaxScores(Persons);
    } else {
      Persons = new ArrayList();
      Persons.add(new PersonBean(getTime(), 0));
      AppendNewPerson(Persons);
    }
  }

  private void getMaxScores(ArrayList<PersonBean> Persons) {
    peps = Persons;
    Collections.sort(peps, new SortByScores());
    if (((PersonBean) peps.get(0)).scores > MaxScores) {
      MaxScores = ((PersonBean) peps.get(0)).scores;
    }
    this.best_s = ((PersonBean) peps.get(0)).scores;

    if (this.best_s < ScoresAction.getMyScores()) {
      this.best_s = ScoresAction.getMyScores();
      this.isNew = true;
    } else {
      this.isNew = false;
    }
  }

  private void LoadSocres() {
    try {
      this.disScr = true;
      int i = ScoresAction.getMyScores();
      for (; this.currentScore < i; this.currentScore += 1) {
        Thread.sleep(70L);
        getMagicanvas().repaint();
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void WhichMEDALS() {
    int currentScore = ScoresAction.getMyScores();
    if (currentScore < MaxScores - 30) this.MEDALS = null; else if (
      currentScore <= MaxScores - 20
    ) this.MEDALS = ((Image) MedalsBoardBean.getMEDALS().get(0)); else if (
      currentScore <= MaxScores - 10
    ) this.MEDALS = ((Image) MedalsBoardBean.getMEDALS().get(1)); else if (
      currentScore <= MaxScores
    ) this.MEDALS = ((Image) MedalsBoardBean.getMEDALS().get(2)); else {
      this.MEDALS = ((Image) MedalsBoardBean.getMEDALS().get(3));
    }
    getMagicanvas().repaint();
    if (this.MEDALS != null) {
      this.medalsOk = true;
      flashStars();
    }
  }

  private void flashStars() {
    try {
      int mw = MedalsBoardBean.getMEDALS_W();
      int mh = MedalsBoardBean.getMEDALS_H();
      int maxSub = MedalsBoardBean.getStar().size();
      while (Switch._isContinue()) {
        this.starX =
          ((int) (mw * Math.random()) + MedalsBoardBean.getMEDALS_X() - 5);
        this.starY =
          ((int) (mh * Math.random()) + MedalsBoardBean.getMEDALS_Y() - 5);
        for (int sub = 0; sub <= maxSub; sub++) {
          if (sub == 0) this.star = null; else {
            this.star = ((Image) MedalsBoardBean.getStar().get(sub - 1));
          }
          getMagicanvas().repaint();
          Thread.sleep(100L);
        }
        Thread.sleep(450L);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static void AppendNewPerson(ArrayList<PersonBean> Persons) {
    Persons.add(new PersonBean(getTime(), ScoresAction.getMyScores()));
    WriteData.WriteObjectInLocal(Persons, "/Scores.dat");
  }

  public static String getTime() {
    SimpleDateFormat df = new SimpleDateFormat("MM-dd/HH:mm");
    String time = df.format(new Date());
    String t = "";
    for (String num : time.substring(0, time.indexOf("/")).split("-")) {
      int i = Integer.parseInt(num);
      if (t.equals("")) t = i; else {
        t = t + "-" + i;
      }
    }
    return t = t + " " + time.substring(time.indexOf("/") + 1);
  }
}
